add_library(nodes SHARED)
set_target_properties(nodes PROPERTIES LINKER_LANGUAGE CXX)

function(make_single_compile_unit fname file_list)
    set(content "// single compilation unit auto-generated by cmake\n")
    foreach (f ${file_list})
        string(APPEND content "#include \"${CMAKE_CURRENT_LIST_DIR}/${f}\"\n")
    endforeach()
    file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${fname} ${content})
endfunction()

option(BUILD_NODES "Whether to build actual nodes (useful to turn off to just run basic tests)" ON)
option(NODES_SCU "Use single compilation unit for nodes (speeds up one time build if you have enough RAM)" OFF)

set(ALL_NODE_SUBSETS)

function(add_node_subset name)
    option("BUILD_${name}_NODES" "Build ${name} node subset?" ON)
    add_library("nodes_${name}" SHARED)
    target_link_libraries("nodes_${name}" document)
    if (BUILD_NODES)
        if (NODES_SCU)
            make_single_compile_unit("nodes_${name}.cpp" "${ARGN}")
            target_sources("nodes_${name}" PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/nodes_${name}.cpp")
        else()
            target_sources("nodes_${name}" PRIVATE ${ARGN})
        endif()
    endif()
    target_link_libraries(nodes "nodes_${name}")
    install(
        TARGETS "nodes_${name}"
        EXPORT rainynite_core
        LIBRARY DESTINATION lib/rainynite/
        ARCHIVE DESTINATION lib/rainynite/
    )
endfunction()

set(RENDERABLE_SOURCES
    renderable/composite.cpp
    renderable/image.cpp
    renderable/transform.cpp
    renderable/text.cpp
    renderable/render_shape.cpp
    renderable/svg.cpp
)

add_node_subset(renderable ${RENDERABLE_SOURCES})

add_node_subset(animated
    animated.cpp
    frame_list.cpp
    interpolate.cpp
    frame.cpp
)

add_node_subset(list
    time_list.cpp
    list.cpp
    sort.cpp
)

add_node_subset(string
    string.cpp
    format_string.cpp
    format_number.cpp
    to_string.cpp
    color_string.cpp
    file_string.cpp
)

add_node_subset(dynamic
    any_proxy.cpp
    dynamic_node.cpp
)

add_node_subset(geom
    morph.cpp
    follow_path.cpp
    path_xy.cpp
    extract_coord.cpp
    point_xy.cpp
    knot.cpp
    knot_list.cpp
    rectangle.cpp
    circle.cpp
)

add_node_subset(transform
    affine.cpp
    transform_composite.cpp
    transform_path.cpp
)

add_node_subset(misc
    get_property.cpp
    bones.cpp
    operators.cpp
    time.cpp
    time_period.cpp
    color.cpp
    color_mix.cpp
    shading.cpp
    linear.cpp
    truncate.cpp
    average.cpp
    math.cpp
    boolean.cpp
    compare.cpp
    conditional.cpp
    split_string.cpp
    random.cpp
    audio.cpp
    test/transform.cpp
    test/benchmark.cpp
)

install(
    TARGETS nodes
    EXPORT rainynite_core
    LIBRARY DESTINATION lib/rainynite/
    ARCHIVE DESTINATION lib/rainynite/
)
